import { route } from 'quasar/wrappers'
import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import routes from './routes'
import { TokenKey } from 'src/common/const'
import type { DynamicRouteProps } from 'src/stores/modules/user'
import { useUserData } from 'src/hooks'
import { MockDynamicRoutes } from './routes'
/*
 * If not building with SSR mode, you can
 * directly export the Router instantiation;
 *
 * The function below can be async too; either use
 * async/await or return a Promise which resolves
 * with the Router instance.
 */
/**
 vite GlobImport 动态导入  需要在tsconfig文件下添加类型定义
   "compilerOptions": {
    "types": ["vite/client"]
  }
 * 
 */
const modules = import.meta.glob("src/pages/**/*.vue");

const createHistory = process.env.SERVER
  ? createMemoryHistory
  : (process.env.VUE_ROUTER_MODE === 'history' ? createWebHistory : createWebHashHistory)

export const Router = createRouter({
  scrollBehavior: () => ({ left: 0, top: 0 }),
  routes,
  // Leave this as is and make changes in quasar.conf.js instead!
  // quasar.conf.js -> build -> vueRouterMode
  // quasar.conf.js -> build -> publicPath
  history: createHistory(process.env.VUE_ROUTER_BASE)
})

Router.beforeEach((to, from, next) => {
  const [userData] = useUserData();
  if (!userData[TokenKey] && !to.fullPath.startsWith('/login')) {
    next(`/login?redirect=${to.path}`)
  }
  next()
})

export const initDynamicRouter = () => {
  const [userData] = useUserData();
  if (!Router.hasRoute('main') && userData.menuVoList && userData.menuVoList.length || MockDynamicRoutes) {
    const originRootRoute: RouteRecordRaw = {
      path: '/',
      redirect: '/clue',
      name: 'main',
      component: () => import('layouts/MainLayout.vue'),
      children:<RouteRecordRaw[]> [
        // {path: 'clue', component: () => import('pages/clueManager/recordOrder/index.vue')},
        // {path: 'clueManage', component: () => import('pages/clueManager/clueManage/index.vue')},
        // {path: 'departmentStatistics', component: () => import('pages/dataStatistics/departmentStatistics/index.vue')},
        // {path: 'orderRank', component: () => import('pages/dataStatistics/orderRank/index.vue')},
        // {path: 'departmentManage', component: () => import('pages/systemManager/departmentManage/index.vue')},
        // {path: 'userManage', component: () => import('pages/systemManager/userManage/index.vue')},
      ]
    }
    MockDynamicRoutes.forEach((item: DynamicRouteProps) => {
      originRootRoute.children.push({
        path: item.route,
        name: 'ok'+item.route,
        component: modules['../' +item.path]
      })
    })
    console.log('originRootRoute ',originRootRoute);
    
    Router.addRoute(originRootRoute);
  }
  console.log(' addRouter after ',Router.hasRoute('main'));
}
// console.log('Router  ',Router.hasRoute('main'));

export default route(function (/* { store, ssrContext } */) {
  initDynamicRouter()
  return Router
})
